


export default class LinkedList {
    constructor() {
        this.head = null;
        this.tail = null;
        this.size = 0;
    }

    isEmpty() {
        return this.size === 0;
    }

    isNotEmpty() {
        return this.size !== 0;
    }

    length() {
        return this.size;
    }

    getLast() {
        return this.tail.element;
    }

    getFirst() {
        return this.head.element;
    }

    addLast(element) {
        let node = {
            element: element,
            next: null,
            prev: null
        };
        if (this.head == null) {
            this.head = node;
        } else {
            this.tail.next = node;
            node.prev = this.tail;
        }
        this.tail = node;
        this.size++;
    }

    addFirst(element) {
        let node = {
            element: element,
            next: null,
            prev: null
        };
        if (this.head == null) {
            this.head = node;
            this.tail = node;
        } else {
            node.next = this.head;
            this.head.prev = node;
            this.head = node;
        }
        this.size++;
    }

    *[Symbol.iterator]() {
        let current = this.head;
        while (current != null) {
            yield current.element;
            current = current.next;
        }
    }

    *reverseIterator() {
        let current = this.tail;
        while (current != null) {
            yield current.element;
            current = current.prev;
        }
    }
}
